package cn.doitedu.rtmk.mgmt.doit39_rtmk_management.service;

import cn.doitedu.rtmk.mgmt.doit39_rtmk_management.dao.PersonDao;
import cn.doitedu.rtmk.mgmt.doit39_rtmk_management.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.List;

@Service
public class PersonService {

    @Autowired
    PersonDao personDao;


    public List<Person> getPersonsByName(String name) throws SQLException {

        List<Person> personList = personDao.selectPersonByName(name);
        return personList;
    }


    public String putPerson(Person person) throws SQLException {

        // 先查询该用户是否已存在
        List<Person> personList = personDao.selectPersonByName(person.getName());
        if(personList == null || personList.isEmpty()){
            int i = personDao.insertPerson(person);

            return i == 0? "插入成功":"插入失败";
        }else{

            int i = personDao.updatePerson(person);
            return i == 0? "更新成功":"更新失败";
        }

    }
}
